package MonthGame.快速幂;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        for(int i=0;i<n;i++){
            int nn = s.nextInt();
            long [][] temp = new long [nn][2];
            for(int j=0;j<nn;j++){
                temp[j][0] = s.nextLong();
                temp[j][1] = s.nextLong();
            }
            long ans = 1;
            for(int j=0;j<nn;j++){
                ans *= MUL(temp[j][0],temp[j][1]);
            }
            String anss = String.valueOf(ans);
            System.out.print(anss.charAt(anss.length()-1));
        }
    }
    public static long MUL(long a,long k){
        long ans = a%10;
        long tempa = a;
        for(int j=0;j<k-1;j++){
            tempa = tempa%10;
            tempa = (long) Math.pow(tempa,a);
            ans = tempa%10;
        }
        return ans;
    }
}
